/**
 * Refer to Spring JPetStore
 */
package cn.fanyamin.web.struts;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.struts.DispatchActionSupport;

import cn.fanyamin.business.WebAppFacade;
import cn.fanyamin.domain.User;

/**
 * @author Walter Fan
 * @see #getWebApp
 * @see org.springframework.web.context.support.WebApplicationContextUtils#getRequiredWebApplicationContext
 * @see org.springframework.web.struts.ActionSupport
 * @since 07/14/2006
 */
public abstract class BaseAction extends DispatchActionSupport {
	
    public static final String ACTIONMAPPING_PATH_PREFIX = "/";
    public static final String ACTIONMAPPING_PATH_PARA_PREFIX = "?";
    public static final String ACTIONMAPPING_PATH_PARA_EQUAL = "=";
    
    //Variable for request
    public static final String REQUEST_PAGENUM_KEY = "pageNum";
    public static final int PAGE_SIZE = 20;
    public static final int PAGE_BEGIN_INDEX = 1;
    public static final String ORDER_TYPE_ASC = "ASC";
    public static final String ORDER_TYPE_DESC = "DESC";

    protected static final String COOKIE_PASSWORD = "ck_password_jwhat";

    protected static final String COOKIE_USERNAME = "ck_username_jwhat";
    /**
     * DOCUMENT ME!
     */
    private WebAppFacade webApp;

    /**
     * DOCUMENT ME!
     */
    private static final Log logger = LogFactory.getLog(BaseAction.class);

    protected String getDefaultMethodName() {
        return "list";
    }

    /**
     * DOCUMENT ME!
     * 
     * @param actionServlet
     *            DOCUMENT ME!
     */
    public void setServlet(ActionServlet actionServlet) {
        super.setServlet(actionServlet);

        WebApplicationContext wac = getWebApplicationContext();
        this.webApp = (WebAppFacade) wac.getBean("webApp");
    }

    /**
     * DOCUMENT ME!
     * 
     * @return DOCUMENT ME!
     */
    protected WebAppFacade getWebApp() {
        return webApp;
    }

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                 HttpServletResponse response) throws Exception {
        if (isCancelled(request)) {
            ActionForward af = cancelled(mapping, form, request, response);

            if (af != null) {
                return af;
            }
        }

        String parameter = "", name = "";

        try {
            // Get the parameter. This could be overridden in subclasses.
            parameter = getParameter(mapping, form, request, response);
            // Get the method's name. This could be overridden in subclasses.
            name = getMethodName(mapping, form, request, response, parameter);
        } catch (ServletException e) {
            name = getDefaultMethodName();
        }

        // Prevent recursive calls
        if ("execute".equals(name) || "perform".equals(name)) {
            String message = messages.getMessage("dispatch.recursive", mapping.getPath());
            log.error(message);
            throw new ServletException(message);
        }

        if (StringUtils.isEmpty(name)) {
            name = getDefaultMethodName();
        }
        // Invoke the named method, and return the result
        return dispatchMethod(mapping, form, request, response, name);
    }

    public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                             HttpServletResponse response) {

        return mapping.findForward("add");
    }
    
    protected User getUserFromCookie(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        User user = new User();
        if ((cookies != null) && (cookies.length > 0)) {
            for (int i = 0; i < cookies.length; i++) {
                if (cookies[i].getName().equals(COOKIE_USERNAME)) {
                    user.setUserName(cookies[i].getValue());
                }
                if (cookies[i].getName().equals(COOKIE_PASSWORD)) {
                    user.setPassword(cookies[i].getValue());
                }
            }
        }
        return user;
    }

}
